import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
import java.net.*;

public class BadugiClient extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;
	public static BadugiClient bc;
	WindowAdapter close = new WindowAdapter()
	{
        public void windowClosing(WindowEvent e) 
		{
			if(out!=null)
			{
				out.println("Q");
			}
            System.exit(0);
        }
    };
    Socket	serwer=null;
	PrintWriter out = null;
	BufferedReader in = null;
	private Panel mainPanel, startPanel;
	private JLabel lName, lHost, lPort, lWait;
	private TextField txtName, txtHost, txtPort;
	private JButton bClear, bConnect, bHuman, bBot;
	private SerwerListener servListener;
	private int quantityPlayers, playerType=0;
	private AllPlayerInfoPanel[] allPlayerPanels;
	private MyCardsPanel myCardsPanel;
	private ButtonPanel buttonPanel;
	private Bot bot;
	public BadugiClient()
	{
		super("Badugi Poker - Client");
		addStartPanel();
		setResizable(false);
		addWindowListener(close);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==bHuman)
		{
			playerType=0;
			bHuman.setEnabled(false);
			bBot.setEnabled(true);
		}
		if(e.getSource()==bBot)
		{
			playerType=7;
			bHuman.setEnabled(true);
			bBot.setEnabled(false);
		}
		if(e.getSource()==bClear)
		{
			txtName.setText("\0");
			txtHost.setText("\0");
			txtPort.setText("\0");
		}
		if(e.getSource()==bConnect)
		{
			try
			{
				serwer=new Socket(txtHost.getText(), Integer.parseInt(txtPort.getText()));
				out = new PrintWriter(serwer.getOutputStream(), true);
				in = new BufferedReader(new InputStreamReader(serwer.getInputStream()));
				servListener = new SerwerListener(in, this);
				servListener.start();
				if(playerType==7)
				{
					bot = new Bot(this);
				}
				out.println("S:"+playerType+":"+txtName.getText());
				addWaitPanel();
			}
			catch(UnknownHostException b)
			{
				System.out.println("Bledna nazwa hosta.");
			}
			catch(IOException b)
			{
				System.out.println("Nie mozna utworzyc gniazda klienta.");
			}
			catch(NumberFormatException ex)
			{
				System.out.println("Wystapil problem z zamiana ciagu znakow na liczbe");
			}
		}
	}
	public void addStartPanel()
	{
		startPanel = new Panel();
		startPanel.setLayout(new GridLayout(5,2));
		lName = new JLabel("Imie: ");
		lName.setFont(new Font("Lucida Console", 0, 22));
		lHost = new JLabel("Host: ");
		lHost.setFont(new Font("Lucida Console", 0, 22));
		lPort = new JLabel("Port: ");
		lPort.setFont(new Font("Lucida Console", 0, 22));
		txtName =  new TextField(10);
		txtName.setFont(new Font("Lucida Console", 0, 22));
		txtHost =  new TextField(10);
		txtHost.setFont(new Font("Lucida Console", 0, 22));
		txtPort =  new TextField(10);
		txtPort.setFont(new Font("Lucida Console", 0, 22));
		bHuman = new JButton("Gracz");
		bHuman.addActionListener(this);
		bHuman.setEnabled(false);
		bBot = new JButton("Bot");
		bBot.addActionListener(this);
		bClear = new JButton("Wyczysc");
		bClear.setFont(new Font("Lucida Console", 0, 22));
		bClear.addActionListener(this);
		bConnect = new JButton("Polacz");
		bConnect.addActionListener(this);
		bConnect.setFont(new Font("Lucida Console", 0, 22));
		this.add(startPanel);
		startPanel.add(lName);
		startPanel.add(txtName);
		startPanel.add(bHuman);
		startPanel.add(bBot);
		startPanel.add(lHost);
		startPanel.add(txtHost);
		startPanel.add(lPort);
		startPanel.add(txtPort);
		startPanel.add(bClear);
		startPanel.add(bConnect);
		this.pack();
	}
	public void addWaitPanel()
	{
		this.remove(startPanel);
		lWait = new JLabel("Oczekiwanie na pozostalych graczy...");
		lWait.setFont(new Font("Lucida Console", 0, 36));
		this.add(lWait);
		this.pack();
		
	}
	public void addMainPanel(int quantityPlayers, int tokens, int[] type, String[] name)
	{
		this.remove(lWait);
		this.quantityPlayers=quantityPlayers;
		if(playerType==0)
		{
			this.setSize(800,192+quantityPlayers*40);
		}
		else
		{
			this.setSize(800,142+quantityPlayers*40);
		}
		mainPanel = new Panel();
		mainPanel.setBackground(new Color(0,172,15));
		mainPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 1, 0));
		this.add(mainPanel);
		JPanel upPanel = new JPanel();
		upPanel.setLayout(new GridLayout(quantityPlayers,1));
		mainPanel.add(upPanel);
		allPlayerPanels = new AllPlayerInfoPanel[6];
		for(int i=0; i<quantityPlayers; i++)
		{
			allPlayerPanels[i] = new AllPlayerInfoPanel(type[i]);
			allPlayerPanels[i].initialize((i+1)+"", name[i], tokens+"");
			upPanel.add(allPlayerPanels[i]);
		}
		myCardsPanel = new MyCardsPanel(this);
		myCardsPanel.setSize(750,100);
		mainPanel.add(myCardsPanel);
		if(playerType==0)
		{
			buttonPanel = new ButtonPanel(this);
		}
		if(playerType==0)
		{
			mainPanel.add(buttonPanel);
		}
		
	}
	public void allMove(int playerMovingId, String bet, String tokens, String action)
	{
		allPlayerPanels[playerMovingId].updateBet(bet);
		allPlayerPanels[playerMovingId].updateTokens(tokens);
		allPlayerPanels[playerMovingId].changeAction(action);
	}
	public void allChangeTime()
	{
		if(playerType==0)
		{
			myCardsPanel.changeTime();
		}
		else
		{
			bot.changeCards();
		}
	}
	public void allChange(int id, int[] changedCards)
	{
		allPlayerPanels[id].changecards(changedCards);
	}
	public void allEndDeal(int winnerId, int[] id, String[][] cards)
	{
		allPlayerPanels[winnerId].changeAction("Win Deal");
		for(int i=0; i<id.length; i++)
		{
			allPlayerPanels[id[i]].showcards(cards[i]);
		}
	}
	public void allNewDeal(int dealer, int smallBlind, int bigBlind)
	{
		int[] resetCards = {0,0,0,0};
		for(int i=0; i<quantityPlayers; i++)
		{
			if(i==dealer)
			{
				allPlayerPanels[dealer].changeType(1);
			}
			else if(i==smallBlind)
			{
				allPlayerPanels[smallBlind].changeType(2);
			}
			else if(i==bigBlind)
			{
				allPlayerPanels[bigBlind].changeType(3);
			}
			else
			{
				allPlayerPanels[i].changeType(0);
			}
			allPlayerPanels[i].changecards(resetCards);
		}
	}
	public void iMove(int playerBet, int maxBet)
	{
		if(playerType==0)
		{
			buttonPanel.turn(playerBet, maxBet);
		}
		else
		{
			bot.turn(playerBet, maxBet);
		}
	}
	public void iNewCards(String[] cards)
	{
		 myCardsPanel.changeCards(cards);
		 if(playerType==7)
		 {
			 bot.newCards(cards);
		 }
	}
	public void iNewDeal(int tokens, int superFunction, int smallBlind)
	{
		if(playerType==0)
		{
			buttonPanel.newDeal(tokens, superFunction, smallBlind);
		}
		else
		{
			bot.newDeal(tokens, superFunction, smallBlind);
		}
	}
	public void iNewDeal(int myTokens, int superFunction)
	{
		if(playerType==0)
		{
			buttonPanel.newDeal(myTokens, superFunction);
		}
		else
		{
			bot.newDeal(myTokens, superFunction);
		}
	}
	
	public void changeRequest(int[] change)
	{
		out.println("C:"+change[0]+":"+change[1]+":"+change[2]+":"+change[3]);
	}
	public void allIn()
	{
		out.println("M:AI");
	}
	public void fold()
	{
		out.println("M:FD");
	}
	public void bet(int value)
	{
		out.println("M:BT:"+value);
	}
	public void check()
	{
		out.println("M:CK");
	}
	public void call(int value)
	{
		out.println("M:CL:"+value);
	}
	public void raise(int value)
	{
		out.println("M:RS:"+value);
	}
	public static void main(String[] args)
	{
		bc = new BadugiClient();
		bc.setVisible(true);
		
	}
}
